function AfterSearch(data) {
  
  for (var i in data.data) {
    data.data[i].status = [data.data[i].status]
  }
  return data;
}


function AfterFormSearch(data) {
  
  console.log("process data-->",data);
  return data;
}

function Substr(...args) {
  if(args[0]) {
    return args[0].substring(0, args[1]).concat("  ...");
  }
  return "";
}

// changed by type(keepalive or normal)
function OnChange(query) {

  // 进入onchange事件
  query = query || {};
  field = query.key;
  newVal = query.value;
  oldVal = query.old;
  params = query.params; // 开发者定义数据
  if(field != 'type') {
    return [];
  }

  let setting = Process("yao.form.Setting", "cmdb.process");
  if (setting && setting.code && setting.message) {
    throw new Exception(setting.message, 500);
  }

  let data ; 

  if(field == 'type' && newVal == 'keepalive'){
      // console.log("----------keepalive---------------");
      setting['form']['sections'][0]['columns'].push({
          "name":"VIP",
          "width": 8
       },{
          "name":"优先级",
          "width": 8
       },{
        "name":"KPID",
        "width": 8
     });
  
      Object.assign(setting['fields']['form'], {"VIP": {
          "bind": "vip", 
          "view": {
              "props": {},
              "type": "Text"
          },
          "edit": {
            "type": "Input",
            "props": {}
          }
      }}, {"优先级": {
          "bind": "priority", 
          "view": {
              "props": {},
              "type": "Text"
          },
          "edit": {
            "type": "Input",
            "props": {}
          }
      }}, {"KPID": {
        "bind": "kp_id", 
        "view": {
            "props": {},
            "type": "Text"
        },
        "edit": {
          "type": "Input",
          "props": {}
        }
    }});
     // data = { ...query, vip: "192.168.127.88",priority:1 }; 
      data = { ...query }; 

  }else{
     // console.log("----------normal---------------");
     delete setting['fields']['form'].VIP
     columns = setting['form']['sections'][0]['columns']
     columns = columns.filter(function(obj) {
      return obj.name !== "VIP";
    });
    data = { ...query }
    delete data.vip
    delete data.priority
    delete data.kp_id
  }
 
  return {
    data: data,
    setting: setting,
  };
}

// args[0] is server_id
function GetUsersByServer(...args){
   if(args[0]){
    let res = Process('models.cmdb.server.Find', args[0]["server_id"],{"select":["account"]});
   // console.log("res---3->",res)
    // const newres = [{label:"N/A",value:"0"}, ...res];
    if(!res.account) return [];
    const newres = res.account.map(item => {
      return {
        label: item.name,
        value: item.name
      };
    });
   // console.log("newres--->",newres);
    return newres;
   }

   return [];
}

function OnChangeByServer(query) {
   console.log("query-OnChangeByServer----->",query)
   // 进入onchange事件
   query = query || {};
   field = query.key;
   newVal = query.value;
   oldVal = query.old;
   params = query.params; // 开发者定义数据
   console.log("query-isOnLoad----->",query.isOnLoad)
   if(query.isOnLoad) {
    
    let data = { ...query, user: ""}; 
    //let data = { query: query };
    //data.user = "";
    let newres = GetUsersByServer({"server_id": query.value});
    const isMatchFound = newres.some(item => item.value === params);
    console.log("isMatchFound===>",isMatchFound); // 输出：true
    if(!isMatchFound){
      let userValue = "";
      if(newres[0]){
         userValue = newres[0]["value"];
      }
      data = { ...query, user: userValue}; 
    }
    //console.log("data-isOnLoad->",data);
    return {data};
   }
  

  return {};
}

function OnChangeByServer1(query) {
  console.log("query-OnChangeByServer----->",query)
  // 进入onchange事件
  query = query || {};
  field = query.key;
  newVal = query.value;
  oldVal = query.old;
  params = query.params; // 开发者定义数据

  let data = { ...query }; 
  console.log(query.isOnload);
  if(field == 'server_id' ){
    //读取form配置
  let newres = GetUsersByServer({"server_id": query.value});

    const isMatchFound = newres.some(item => item.value === params);
    console.log("isMatchFound===>",isMatchFound); // 输出：true
    if(!isMatchFound){
      let userValue = "";
      if(newres[0]){
         userValue = newres[0]["value"];
      }
      data = { ...query, user: userValue}; 
    }
  
 }

 return {
   data: data
 };
}